package com.foreverwin.jzymes.eap.Listener;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.framework.annotation.MessageLog;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.util.DateUtil;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class AgvListener {

    @Autowired
    JmsMessagingTemplate jmsTemplate;

    /**
     * 支动线接驳站信息
     *
     */
    @JmsListener( destination = "MES_AGV_DISPATCH" )
    @MessageLog
    public void publishAgvInfo( String agvDispatchJson ){
        try {
            JSONObject map = JSONObject.parseObject( agvDispatchJson );
            JSONObject agvMap = new JSONObject();
            agvMap.put( "id", map.getString( "ID" ) );
            agvMap.put( "status", map.getString( "STATUS" ) );
            agvMap.put( "source", map.getString( "SOURCE" ) );
            agvMap.put( "target", map.getString( "TARGET" ) );

            JSONArray agvs = new JSONArray();
            agvs.add( agvMap );

            JSONObject valueMap = new JSONObject();
            valueMap.put( "time", DateUtil.format( new Date(), "yyyy-MM-dd HH:mm:ss" ) );
            valueMap.put( "agvs", agvs );

            JSONObject eventData = new JSONObject();
            eventData.put( "CHANNEL", map.get( "CHANNEL" ) );
            eventData.put( "MESSAGE_TYPE", "WS_AGV_DISPATCH" );
            eventData.put( "VALUE", valueMap );
            AppEvent<JSONObject> appEvent = new AppEvent<>( this );
            appEvent.setName( "AGV_DISPATCH" );
            appEvent.setData( eventData );
            ActiveMQTopic topic = new ActiveMQTopic( "MES_APP_EVENT_INFRA" );
            jmsTemplate.convertAndSend( topic, appEvent );

        } catch (Exception e) {
            throw e;
        }

    }
}
